import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
// import {userInfo} from '@/views/userInfo.js'
import { userInfo } from '@/stores/default'
// const user =userInfo()


const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      redirect: '/home',
    },
    {
      path: '/home',
      name: 'home',
      redirect: '/index',
      component: HomeView,
      children: [
        {
          path: '/index',
          name: 'index',
          component: () => import('../views/IndexView.vue'),
        },
        {
          path: '/chuanbo',
          name: 'chuanbo',
          component: () => import('../views/ChuanboView.vue'),
        },
        {
          path: '/gangkou',
          name: 'gangkou',
          component: () => import('../views/GangkouView.vue'),
        },
        {
          path: '/hangcheng',  
          name: 'hangcheng',
          component: () => import('../views/HangchengView.vue'),
        },
        {
          path: '/system',
          name: 'system',
          component: () => import('../views/SystemView.vue'),
          redirect: '/user',
          children: [
            {
              path: '/user',
              name: 'user',
              component: () => import('../components/system/User.vue'),
            },
            {
              path: '/role',
              name: 'role',
              component: () => import('../components/system/Role.vue'),
            },
            {
              path: '/right',
              name: 'right',
              component: () => import('../components/system/Right.vue'),
            },
            {
              path: '/setting',
              name: 'setting',
              component: () => import('../components/system/Setting.vue'),
            },
            {
              path: '/log',
              name: 'log',
              component: () => import('../components/system/Log.vue'),
            },
          ]
        },
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/LoginView.vue'),
    }
  ]
})


function getcookie(objname) {//获取指定名称的cookie的值

  var arrstr = document.cookie.split("; ");

  for (var i = 0; i < arrstr.length; i++) {

    var temp = arrstr[i].split("=");

    if (temp[0] == objname) return decodeURI(temp[1]);

  }

}
// 前置守卫
router.beforeEach((to, from, next) => {
  // let rightArr=userInfo().getValue.rightArr
  // console.log('rightArr',rightArr);
  

  // 获取sessionStorage中的userId
  // if (to.path.includes('chunde') || to.path.includes('wx')|| to.path.includes('wxsa')) {
  //   return next()
  // }
  // const userId = sessionStorage.getItem('userId')

  // if (to.path === '/login') {
  //   if (userId) {
  //     return next('/home')
  //   }
  //   return next()
  // }
  // if (!userId) return next('/login')
  // console.log(rightArr.join(',').includes(to.matched[1].path.split('/')[1]));
  
  next()
})

export default router
